<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="问题名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="questionName">
              <a-input v-model="model.questionName" placeholder="请输入问题名称" style="width: 80%" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问题类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="questionType">
              <j-dict-select-tag style="width: 80%"
                placeholder="请选择问题类型"
                dictCode="space_question_question_type"
                v-model="model.questionType"
              />
            </a-form-model-item>
          </a-col>

          <!--<a-col :span="24">
            <a-form-model-item label="问题选项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="questionOptions">
              <a-input v-model="model.questionOptions" placeholder="请输入问题选项" style="width: 80%" >
              </a-input>
              <a-button type="primary" @click="addQuestionOption(model.questionOptionsList)">添加项</a-button>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="问题选项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="questionOptions">
              <div v-for="(item,index) in model.questionOptionsList">
                <a-input v-model="model.questionOptionsList[index]"  :key="index" placeholder="请输入问题选项" style="width: 80%" ></a-input>
                <a-button type="primary" style="margin-left: 10px" @click="deleteItem(index,model.id)">删除项</a-button>
              </div>
              <a-button type="primary" @click="addQuestionOption(model.questionOptionsList)">添加项</a-button>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction ,deleteAction,putAction} from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'GhSpaceQuestionForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          questionOptionsList:['']
        },
        currentSpaceId: '',
        startIndex:0,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          questionName: [{ required: true, message: '问题名称不能为空' }],
          questionType: [{ required: true, message: '问题类型不能为空' }],
        },
        url: {
          add: "/ghSpaceQuestion/add",
          edit: "/ghSpaceQuestion/edit",
          queryById: "/ghSpaceQuestion/queryById",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (spaceId) {
        this.visible = true;
        this.model.spaceId = spaceId;
        //this.edit(this.modelDefault);
        this.model.questionOptionsList = [""]
      },
      edit (record) {
        this.model = Object.assign({},{spaceId:''}, record);
        this.startIndex = this.model.questionOptionsList.length;
        this.visible = true;
      },
      deleteItem(index,id){
        this.model.questionOptionsList.splice(index,1)
        if (index < this.startIndex){
          let params = {};
          params.id = id;
          params.questionOptionsList = this.model.questionOptionsList;
          putAction(this.url.edit, params).then((res) => {
            if (res.success) {
              this.$message.success("删除成功")
              that.$emit('ok');
            } else {
              this.$message.warning("删除失败")
            }
          })
        }
      },
      addQuestionOption(arr){
        arr.push("")
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            if (this.model.questionOptionsList[0] == null || this.model.questionOptionsList[0] == ''){
              this.$message.error("请至少添加一个有效选项")
              return;
            }
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>